package com.iuie.ns.datum.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.iuie.ns.datum.service.IFileInfoService;
import com.iuie.ns.datum.service.IFileService;
import com.iuie.ns.datum.vo.FileInfoVO;
import com.iuie.ns.datum.vo.FileTagVO;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;

@RequestMapping("datum/file")
@Controller
public class FileController {
	
	@Resource
	private IFileService fileService;
    
    @Resource
    private IFileInfoService fileInfoService;
	
	@RequestMapping
	public String index(){
		return "view-datum/file/fileList";
	}

	/**
	 * 根据文件标签查询文件列表
	 * @param page
	 * @param fileTag
	 * @return
	 * @date 2017年8月15日 上午10:52:00
	 */
	@RequestMapping(value="findByTag",produces="application/json; charset=utf-8")
	@ResponseBody
	public String FindByTag(Page page,FileTagVO fileTag){
		List<FileInfoVO> list = fileInfoService.findByTag(null,fileTag);
		
		ResponseInfo ri = new ResponseInfo();
		ri.setInfo(list);
		String json = ri.toJson();
		return json;
	}
	
	/**
	 * 文件上传
	 * @param file 上传的文件
	 * @param request http servlet request
	 * @param fileType 文件归属，blog/file
	 * @param func 上传完成后，要执行的js函数
	 * @return
	 * @date 2017年8月15日 上午10:51:17
	 */
	@RequestMapping(value="/doUpload",method=RequestMethod.POST)
	@ResponseBody
	public String doUploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request, String fileType, String func){
		String json = null;
		try {
			String s = request.getParameter("immediately");
			Boolean immediately = s == null ? true : Boolean.valueOf(s);
			FileInfoVO fileInfo = fileService.addOneFile(file, immediately, fileType);
			ResponseInfo ri = new ResponseInfo();
			ri.setInfo(fileInfo);
			json = ri.toJson();
			// 将json加工为javascript能够识别的字符串
		} catch (IOException e) {
			json = ResponseInfo.errorJson(e.getMessage());
		}
		String jsJson = StringEscapeUtils.escapeJavaScript(json);
		String html = "<script type='text/javascript'>" + func + "('"+jsJson+"')</script>";
		return html;
	}
	
	/**
	 * 分页查询文件列表
	 * @param fileInfo 查询条件
	 * @param page 分页信息
	 * @return
	 * @date 2017年8月15日 上午10:54:10
	 */
	@RequestMapping(value="/findByPage",method=RequestMethod.GET)
	@ResponseBody
	public ResponseInfo findByPage(FileInfoVO fileInfo, Page page) {
	    List<FileInfoVO> list = fileInfoService.queryLst(page, fileInfo);
	    return ResponseInfo.successInfo(list, page);
	}
	
	@RequestMapping(value="/fileDelete/{fileId}",method=RequestMethod.GET)
	@ResponseBody
    public ResponseInfo fileDelete(@PathVariable("fileId") Long fileId){
        fileService.deleteFileById(fileId);
        return ResponseInfo.successInfo("删除文件成功！");
    }
	
    @RequestMapping(value="/updateDownloadCount/{fileId}/{row}",method=RequestMethod.GET)
    public @ResponseBody ResponseInfo updateDownloadCount(@PathVariable("fileId") Long fileId, @PathVariable("row") Integer row){
        fileInfoService.downloadFileById(fileId);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("fileId", fileId);
        map.put("row", row);
        return ResponseInfo.successInfo("更新下载次数成功！", map);
    }
    
    @RequestMapping(value="/fileDownload",method=RequestMethod.GET)
    public ResponseEntity<byte[]> fileDownload(Long fileId, Boolean download, HttpServletRequest request){
        ResponseEntity<byte[]> respResult = null;
        try {
            respResult = fileService.fileDownload(fileId,download);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return respResult;
    }
}
